require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../lib/flexible')
require('../font/iconfont.css')
    //引入axios
const axios = require('axios')
require('../css/login.less')

//weui-插件库
require('weui')
const weui = require('weui.js')

const phone = document.querySelector('.phone')
const password = document.querySelector('.password')
const btnRegister = document.querySelector('.btnRegister')
const errMsg = document.querySelector('#errMsg') //错误提示

btnRegister.addEventListener('click', function() {
    // 这里的inputvalue变量值要写在click事件里面,(点击后才获取值)
    const phoneVal = phone.value
    const passwordVal = password.value
    if (!/^1[3-9][0-9]{9}$/.test(phoneVal)) {
        errMsg.style.display = 'block'
        errMsg.textContent = '手机号错误'
        return
    } else if (!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)) {
        errMsg.style.display = 'block'
        errMsg.textContent = '密码错误'
        return
    } else {
        errMsg.textContent = ''
            // phone.value = password.value = ''
        axios.post('http://139.9.177.51:8099/users/login', {
            "account": phoneVal,
            "password": passwordVal
        }).then(function(res) {
            if (res.data.status === 0) {
                //将用户userId存入本地存储
                localStorage.setItem('userId', res.data.data.user.userId)

                weui.toast('登录成功', {
                    duration: 1000,
                    className: 'custom-classname', //自定义类名
                    callback: function() { //回调函数
                        location.href = './index.html'
                    }
                });
                // console.log(res.data);
            } else {
                weui.alert(res.data.msg)
            }

        })
    }



})